package com.wzb.mybatisplus.bitaction.electrumaction.five;

import com.wzb.mybatisplus.bitaction.electrumaction.FileUtils;
import org.bitcoinj.core.*;
import org.bitcoinj.params.MainNetParams;
import java.math.BigInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicInteger;

public class QuickParallelFinder {

    private static final NetworkParameters params = MainNetParams.get();
    private static final AtomicLong counter = new AtomicLong(1L);
    private static final AtomicInteger found = new AtomicInteger(0);
    private static String path = "D:\\btc\\miyao\\maney5.txt";
    private static FileUtils fileUtils = new FileUtils();
    public static void main(String[] args) throws InterruptedException {
        System.out.println("快速多线程查找 - 从1开始顺序查找以5开头的私钥\n");

        int threadCount = 4;
        int targetCount = 5;

        Thread[] threads = new Thread[threadCount];
        long startTime = System.currentTimeMillis();

        for (int i = 0; i < threadCount; i++) {
            threads[i] = new Thread(new QuickWorker(i, targetCount, startTime));
            threads[i].start();
        }

        for (Thread thread : threads) {
            thread.join();
        }

        System.out.printf("\n✅ 完成! 总共找到 %d 个私钥，总耗时: %.1f秒%n",
                found.get(), (System.currentTimeMillis() - startTime) / 1000.0);
    }

    static class QuickWorker implements Runnable {
        private final int id;
        private final int target;
        private final long startTime;

        public QuickWorker(int id, int target, long startTime) {
            this.id = id;
            this.target = target;
            this.startTime = startTime;
        }

        @Override
        public void run() {
            while (found.get() < target) {
                long current = counter.getAndIncrement();

                try {
                    ECKey key = ECKey.fromPrivate(BigInteger.valueOf(current));
                    String wif = key.getPrivateKeyAsWiF(params);

                    if (wif.startsWith("5")) {
                        int count = found.incrementAndGet();
                        if (count <= target) {
                            Address addr = LegacyAddress.fromKey(params, key);
                            double elapsed = (System.currentTimeMillis() - startTime) / 1000.0;

                            fileUtils.writeFile("数值:"+current+"  私钥:"+wif +"地址:"+addr ,path);
                            System.out.printf("\n🔥 找到第%d个! (Worker%d)%n", count, id);
                            System.out.printf("   数值: %,d%n", current);
                            System.out.printf("   私钥: %s%n", wif);
                            System.out.printf("   地址: %s%n", addr);
                            System.out.printf("   耗时: %.1f秒%n", elapsed);
//                            System.out.println("   " + "-".repeat(50));
                        }
                    }

                } catch (Exception e) {
                    // 继续下一个
                }
            }
        }
    }
}